<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import dayjs from 'dayjs'
import { getAction } from '@api/manage'
import WorkInboundRecordModal from './modules/WorkInboundRecordModal.vue'
import WorkInboundRecordLabelListPrintModal
  from './modules/WorkInboundRecordLabelListPrintModal.vue'
import WorkInboundRecordLabelGeneratePrintModal
  from './modules/WorkInboundRecordLabelGeneratePrintModal.vue'
import PlanItemList from '@views/qms/plan/PlanItemList.vue'

export default {
  name: 'WorkPlanList',
  components: {
    PlanItemList,
    WorkInboundRecordLabelGeneratePrintModal,
    WorkInboundRecordLabelListPrintModal,
    WorkInboundRecordModal
  },
  mixins: [JeecgListMixin],
  data() {
    return {
      columns: [
        {
          title: '计划单号',
          align: 'center',
          dataIndex: 'code',
          sorter: (a, b) => {
            const valA = a.code || ''
            const valB = b.code || ''
            return valA.localeCompare(valB, 'en')
          },
        },
        {
          title: '生产车间',
          align: 'center',
          dataIndex: 'workshopCode_dictText',
          sorter: (a, b) => {
            const valA = a.workshopCode_dictText || ''
            const valB = b.workshopCode_dictText || ''
            return valA.localeCompare(valB, 'en')
          },
        },
        {
          title: '执行日期',
          align: 'center',
          dataIndex: 'workTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          },
          sorter: (a, b) => {
            const valA = a.workTime || ''
            const valB = b.workTime || ''
            return valA.localeCompare(valB, 'en')
          },
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'createBy_dictText',
          sorter: (a, b) => {
            const valA = a.createBy_dictText || ''
            const valB = b.createBy_dictText || ''
            return valA.localeCompare(valB, 'en')
          },
        },
        {
          title: '制单部门',
          align: 'center',
          dataIndex: 'sysOrgCode_dictText',
          sorter: (a, b) => {
            const valA = a.sysOrgCode_dictText || ''
            const valB = b.sysOrgCode_dictText || ''
            return valA.localeCompare(valB, 'en')
          },
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'createTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          },
          sorter: (a, b) => {
            const valA = a.createTime || ''
            const valB = b.createTime || ''
            return valA.localeCompare(valB, 'en')
          },
        },
        // isChanged
        {
          title: '是否变更计划',
          align: 'center',
          dataIndex: 'isUpdate_dictText',
          sorter: (a, b) => {
            const valA = a.isUpdate_dictText || ''
            const valB = b.isUpdate_dictText || ''
            return valA.localeCompare(valB, 'en')
          },
        }
      ],
      url: {
        list: '/pro/tslProWorkshopPlan/queryWorkPlanList'
      },
      queryParam: {
        createTimeBegin: dayjs().subtract(7, 'day').format('YYYY-MM-DD'),
        createTimeEnd: dayjs().format('YYYY-MM-DD')
      },
      planItemMainId: ''
    }
  },
  methods: {
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      this.planItemMainId = selectionRows[0]['id']
    },
    handlePrint() {
      if (!this.selectValidate()) {
        return
      }
      this.$refs.printModalForm.print(this.selectionRows[0])
      this.$refs.printModalForm.title = '标签打印'
    },
    handleLabelPrint() {
      if (!this.selectValidate()) {
        return
      }
      this.$refs.labelPrintModalForm.list(this.selectionRows[0])
      this.$refs.labelPrintModalForm.title = '标签补打'
    },
    selectValidate() {
      if (!this.selectionRows || this.selectionRows.length === 0) {
        this.$message.warning('请先选中一条记录')
        return false
      }
      if (this.selectionRows.length > 1) {
        this.$message.error('最多只能选择一条记录')
        return false
      }
      return true
    },
    handleAdd() {
      if (!this.selectionRows || this.selectionRows.length === 0) {
        this.$message.warning('请先选中一条记录')
        return
      }
      this.$refs.modalForm.edit(this.selectionRows[0])
      this.$refs.modalForm.title = '添加'
      this.$refs.modalForm.disableSubmit = false
    }
  }
}
</script>

<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item label="制单日期">
              <j-date
                :show-time="true"
                date-format="YYYY-MM-DD"
                placeholder="请选择开始时间"
                class="query-group-cust"
                v-model="queryParam.createTimeBegin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="true"
                date-format="YYYY-MM-DD"
                placeholder="请选择结束时间"
                class="query-group-cust"
                v-model="queryParam.createTimeEnd"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">添加</a-button>
      <a-button type="primary" icon="printer" @click="handlePrint">标签打印</a-button>
      <a-button type="primary" icon="printer" @click="handleLabelPrint">标签补打</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap1"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type:'radio'}"
        :customRow="clickThenSelect"
        @change="handleTableChange">
      </a-table>
    </div>
    <plan-item-list :mainId="planItemMainId" />

    <work-inbound-record-modal ref="modalForm" @ok="modalFormOk" />
    <work-inbound-record-label-list-print-modal ref="labelPrintModalForm" />
    <work-inbound-record-label-generate-print-modal ref="printModalForm" />
  </a-card>
</template>

<style scoped>

</style>